<template>
  <el-dialog title="物料产品选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal=true
             width="80%"
             :append-to-body="true"
             center
  >
    <el-row :gutter="20">
      <!--物料数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="计划编号" prop="planNumber">
            <el-input
              v-model="queryParams.planNumber"
              placeholder="请输入计划编号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="到货日期" prop="materialDeadline">
            <el-date-picker
              v-model="queryParams.materialDeadline"
              type="date"
              placeholder="选择到货日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="物料名称" prop="itemName">
            <el-input
              v-model="queryParams.itemName"
              placeholder="请输入物料名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table border ref="itemTable" v-loading="loading" :data="itemList" @select="handleSelect" @select-all="handleAllSelect"
                  :row-key="getRowKeys" height="650">
          <el-table-column
            type="selection"
            :reserve-selection="true"
            width="55">
          </el-table-column>
          <el-table-column label="采购计划号" width="120" align="center" key="planNumber" prop="planNumber"
                           v-if="columns[0].visible"/>
          <el-table-column label="物料编码" width="120" align="center" key="itemCode" prop="itemCode"
                           v-if="columns[1].visible"/>
          <el-table-column label="物料名称" min-width="120" align="left" key="itemName" prop="itemName"
                           v-if="columns[2].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="规格型号" align="left" key="specification" prop="specification"
                           v-if="columns[3].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="单位" align="center" key="unitOfMeasure" prop="unitOfMeasure"
                           v-if="columns[4].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="所属分类" align="center" key="itemTypeName" prop="itemTypeName"
                           v-if="columns[5].visible" width="120"/>
          <el-table-column
            v-for="col in columnsConfig"
            :key="col.prop"
            :label="col.label"
            :prop="col.prop"
            :width="col.width"
            :align="col.align"
            v-if="col.show">
          </el-table-column>
          <el-table-column label="客户" align="center" prop="clientName" width="80" v-if="columns[10].visible" fixed="right"/>
          <el-table-column label="备注" align="center" prop="remark" width="130" v-if="columns[12].visible"
                           fixed="right"/>
          <el-table-column label="未采购数量" align="center" prop="planQuantity" v-if="columns[11].visible" width="160"
                           fixed="right">
            <template slot-scope="scope">
              {{ scope.row.planQuantity - scope.row.purchasedQuantity }}
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getNotPurchasedPlanItem} from "@/api/mes/cg/plan";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";


export default {
  name: "CgItemSingle",
  dicts: ['sys_yes_no', 'mes_item_product'],
  props: {
    purchasedQuantity: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      showFlag: false,
      selectedRows1: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planNumber: undefined,
        materialDeadline: undefined,
        itemName: undefined,
      },
      // 物料产品表格数据
      itemList: [],
      // 列信息
      columns: [
        {key: 0, label: '采购计划号', visible: true},
        {key: 1, label: '物料/产品编码', visible: true},
        {key: 2, label: '物料/产品名称', visible: true},
        {key: 3, label: '规格型号', visible: true},
        {key: 4, label: '单位', visible: true},
        {key: 5, label: '物料分类', visible: true},
        {key: 6, label: '品牌', visible: true},
        {key: 7, label: '颜色', visible: true},
        {key: 8, label: '技术要求', visible: true},
        {key: 9, label: '型号', visible: true},
        {key: 10, label: '图号', visible: true},
        {key: 11, label: '采购数量', visible: true},
        {key: 12, label: '备注', visible: true},

      ],
      columnsConfig:[],
    };
  },
  created() {
    this.getItemBase();
    /*this.getList();*/
  },
  methods: {
    getRowKeys(row){
      return row.planDetailId;
    },
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      getNotPurchasedPlanItem(this.queryParams).then(response => {
          if (response.rows && response.rows.length > 0) {
            response.rows.forEach(item => {
              //获取已采购数量
              if (this.purchasedQuantity[item.planDetailId]) {
                item.purchasedQuantity = item.purchasedQuantity - this.purchasedQuantity[item.planDetailId];
              }
            })
          }
          this.itemList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.$nextTick(function () {
            if (this.selectedRows1.length > 0) {
              this.selectedRows1.forEach(row => {
                let data = this.itemList.find(item => item.planDetailId == row.planDetailId);
                if (data) {
                  this.$refs.itemTable.toggleRowSelection(data,true);
                }
              })
            }
          })
        }
      );
    },
    /*handleSelectionChange(val) {
      if (val && val.length > 0) {
        val.forEach((row, index) => {
          row.quantity = row.planQuantity - row.purchasedQuantity;
        })
      }
      this.selectedRows = val;

    },*/
    handleSelect(selection, row) {
      let index=this.selectedRows1.findIndex(item => item.planDetailId === row.planDetailId);
      if(index==-1){
        row.quantity = row.planQuantity - row.purchasedQuantity;
        this.selectedRows1.push(row);
      }else{
        this.selectedRows1.splice(index,1);
      }
    },
    handleAllSelect( selection){
      if(selection.length>0){
        const u= selection.filter(item=>
          !this.selectedRows1.some(selectedItem=>selectedItem.itemId===item.itemId)
        ).map(item=>{
          item.quantity = item.planQuantity - item.purchasedQuantity;
          return item;
        });
        this.selectedRows1=[...this.selectedRows1,...u];
      }else{
        const u= this.selectedRows1.filter(item=>
          !this.itemList.some(selectedItem=>selectedItem.itemId===item.itemId)
        )
        this.selectedRows1=u;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRows1 == null || this.selectedRows1.length == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows1);
      this.showFlag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.itemName=null;
      this.queryParams.planNumber=null;
      this.queryParams.materialDeadline=null;
      this.handleQuery();
    },
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },

  },
};
</script>
